import math


class ExpertSystem:
    """
    专家系统
    """
    def __init__(self) -> None:
        pass

    def create_next_step(self, car_world_angle, car_world_x, car_world_y, target_world_x, target_world_y):
        done, angle, x, y = self.get_next_step(car_world_x, car_world_y, target_world_x, target_world_y)
        distance = math.sqrt(x**2 + y**2)
        angle -= car_world_angle
        angle_direction = 1
        distance_direction = 0
        if angle != 0:
            angle_direction = 1
            if angle < 0:
                angle_direction = 0
                angle = -angle
        return done, angle_direction, angle, distance_direction, distance

    def get_next_step(self, car_world_x, car_world_y, target_world_x, target_world_y):
        if target_world_x > car_world_x:
            if target_world_y > car_world_y:  # ↗
                x = target_world_x - car_world_x
                y = target_world_y - car_world_y
                c = math.sqrt(x**2 + y**2)
                angle = self.asin(x / c)
                return False, angle, x, y
            if target_world_y == car_world_y:  # →
                x = target_world_x - car_world_x
                return False, 90, x, 0
            if target_world_y < car_world_y:  # ↘
                x = target_world_x - car_world_x
                y = car_world_y - target_world_y
                c = math.sqrt(x**2 + y**2)
                angle = 180 - self.asin(x / c)
                return False, angle, x, y
            pass
        if target_world_x == car_world_x:
            if target_world_y > car_world_y:  # ↑
                y = target_world_y - car_world_y
                return False, 0, 0, y
            if target_world_y == car_world_y:  # x
                return True, 0, 0, 0
            if target_world_y < car_world_y:  # ↓
                y = car_world_y - target_world_y
                return False, 180, 0, y
        if target_world_x < car_world_x:
            if target_world_y > car_world_y:  # ↖
                x = car_world_x - target_world_x
                y = target_world_y - car_world_y
                c = math.sqrt(x**2 + y**2)
                angle = 360 - self.asin(x / c)
                return False, angle, x, y
            if target_world_y == car_world_y:  # ←
                x = target_world_x - car_world_x
                return False, 270, x, 0
            if target_world_y < car_world_y:  # ↙
                x = car_world_x - target_world_x
                y = car_world_y - target_world_y
                c = math.sqrt(x**2 + y**2)
                angle = 180 + self.asin(x / c)
                return False, angle, x, y
            pass
        return True, 0, 0, 0

    def asin(self, x: float) -> float:
        """
        计算反正弦
        参数：
            x：数值
        返回：
            x的反正弦角度值
        """
        return math.degrees(math.asin(x))

    def acos(self, x: float) -> float:
        """
        计算反余弦
        参数：
            x：数值
        返回：
            x的反余弦角度值
        """
        return math.degrees(math.acos(x))

    pass
